package com.hanagent.utils;

import java.math.BigDecimal;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class MoneyUtils {
	public static double getCanPostal(int agentId) {
		//获取用户最新分红总额
		double canPostal = getAllMoneyCount(agentId) - getAllPostalMoney(agentId) - getWaitPostalMoney(agentId);
		return canPostal;
	}
	
	public static double getAllPostalMoney(int agentId) {
		Record allPostalMoneySum = Db.findFirst("select sum(postalMoney) as allPostalMoney from Postals where state = 1 and userId="+agentId);
		double allPostalMoney = 0; 
		if (allPostalMoneySum.get("allPostalMoney")!=null) {
			allPostalMoney = allPostalMoneySum.getDouble("allPostalMoney");
		}
		return allPostalMoney;
	}
	
	public static double getWaitPostalMoney(int agentId) {
		Record allWaitPostalMoneySum = Db.findFirst("select sum(postalMoney) as allPostalMoney from Postals where state = 0 and userId="+agentId);
		double allWaitPostalMoney = 0; 
		if (allWaitPostalMoneySum.get("allPostalMoney")!=null) {
			allWaitPostalMoney = allWaitPostalMoneySum.getDouble("allPostalMoney");
		}
		return allWaitPostalMoney;
	}
	
	public static double getAllMoneyCount(int agentId) {
		Record moneyCountSum = Db.findFirst("select sum(moneyCount) as allMoneyCount from MoneyRecords where userOrAgentId = "+ agentId 
				+ " and (type<" + ConstantUtils.MonneyTypeAixin +" or type="+ ConstantUtils.MonneyTypeBalanceExChange +" or type = " + ConstantUtils.MonneyTypeBalancePay +")");
		double allMoneyCount = 0; 
		if (moneyCountSum.get("allMoneyCount")!=null) {
			allMoneyCount = moneyCountSum.getDouble("allMoneyCount");
		}
		return allMoneyCount;
	}
	
	public static double formatDouble(double doubleValue) {
		BigDecimal b = new BigDecimal(doubleValue);  
		double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return f1;
	}
	
	
	
	
}
